const puppeteer = require("puppeteer");
var myRequest = require('request');
const utils     = require('../util/utils');
const config    = require('../config/config')



class testWeb{
    constructor() {
    }

    async loadWindow(){
        this.browser = await puppeteer.launch({
            headless: false,
            defaultViewport: {
                width: 0,
                height: 0
            },
            ignoreDefaultArgs: ['--enable-automation','--disable-extensions'],
            args:[
                '--disable-web-security', //解决跨域问题
                `--proxy-server=http://${config.PROXY.HOST}:${config.PROXY.PORT}`,
                `–default-browser-check` //不检查默认浏览器
            ],
        });
        this.page = await this.browser.newPage();

        await this.closeFirstPage();

        // await this.page.setRequestInterception(true);

        await this.camouflageBrowser();

        // this.page.on('request',async (request) => {
        //     console.log(`Url:${request.url()}`)
        //     request.continue();
        // });
        //
        // this.page.on('response',async (response) => {
        //     console.log(response.request().resourceType());
        // })

        await this.page.goto('https://keepa.com/#!');
    }

    async closeFirstPage(){
        let pages = await this.browser.pages();
        if(!pages || pages.length === 0){
            return;
        }
        await pages[0].close();
    }

    // async addCookies(domain){
    //     let cookie = 'optimizelyEndUserId=oeu1600741535219r0.6344778130073587; _vwo_uuid_v2=D31CE1AFB7AEEA2B9AF7B74123DD8E21F|2cd52520e0288d898408dc702c6ac6c7; __zlcmid=10JjSC6lHzs3CGG; __stripe_mid=f2b2eb5d-fad9-4520-ba23-45b93b395c3e8e3ede; _hjid=2c3d74b6-5826-4382-a8c8-1ccb6b2b4a90; _ga=GA1.3.1756808790.1600741536; __ssid=67d0e7ccd9d0752357f2f748ab5484e; _omappvp=Zlr8w4VTEBELBtcFapZBoe3ETqwUuYvhyAlaFKlkpx1CH9HBMyLbfAIcwAb2k0ULREzB0ZYp5MrvkOveMfLc8UNCmk5POo9I; _fbp=fb.1.1600918797451.101308569; _rdt_uuid=1600918798810.472dfeeb-c990-49bb-8f52-7033c10c91aa; _pin_unauth=dWlkPU5HUmtZVEU1TVdFdFlURXpNUzAwWW1RMUxUZ3pPVEF0WXpSak5tVmxNVFZtTkRNeg; _vwo_uuid_v2=D31CE1AFB7AEEA2B9AF7B74123DD8E21F|2cd52520e0288d898408dc702c6ac6c7; _vis_opt_test_cookie=1; _vis_opt_s=1%7C; _gcl_au=1.1.2122107006.1615298775; _rdt_uuid=1600918798810.472dfeeb-c990-49bb-8f52-7033c10c91aa; _hjTLDTest=1; userId=631935; js-fullstory=true; userEmail=gth313@163.com; membershipType=Web App Pro Monthly; extensionType=None; __cfduid=da87d0512496f5c3721cf297b2ed191e81615342258; ajs_anonymous_id=%2255d60c14-f610-42de-91d3-0db38f17a011%22; _gid=GA1.3.206894639.1615465436; _gid=GA1.2.206894639.1615465436; auth_token=eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MTYwNzAzMDYsImlhdCI6MTYxNTQ2NTUwNiwiaXNzIjoianVuZ2xlc2NvdXRfYXBpIiwiYXVkIjoiY2xpZW50IiwiYXV0aF90b2tlbiI6IjkwY2E4NTg0MmNhMTc5YmM2N2VjOTgzYWQzNTBmYmFmIn0.T8HWnyani1tZ20KIlC1KZRePCuWVExa35sPTydP-DHA; ajs_user_id=631935; _hjIncludedInSessionSample=1; _hjAbsoluteSessionInProgress=0; __stripe_sid=2fbb67fb-cdcb-4e92-a46d-cf47f3c1f49694a1a3; mp_c74f4284138a8d26cd2c3b5ebba43d0f_mixpanel=%7B%22distinct_id%22%3A%20%22178213e4d64bb2-096d5640179497-930346c-1fa400-178213e4d65b51%22%2C%22%24device_id%22%3A%20%22178213e4d64bb2-096d5640179497-930346c-1fa400-178213e4d65b51%22%2C%22%24initial_referrer%22%3A%20%22https%3A%2F%2Fwww.junglescout.com%2F%22%2C%22%24initial_referring_domain%22%3A%20%22www.junglescout.com%22%7D; _uetsid=a55d0200826411eba00e9510c0ad5797; _uetvid=a55d5ec0826411eb866583bddf867973; _ga_9C02FJRLCF=GS1.1.1615468460.6.1.1615469276.0; _ga=GA1.2.1756808790.1600741536; _dd_s=rum=0&expire=1615470490253';
    //     let cookies = cookie.split(';').map(
    //         pair => {
    //             let name = pair.trim().slice(0, pair.trim().indexOf('='));
    //             let value = pair.trim().slice(pair.trim().indexOf('=') + 1);
    //             return {name, value, domain}
    //         });
    //     console.log(`Cookies => ${JSON.stringify(cookies)}`);
    //     await Promise.all(cookies.map(pair => {
    //         return this.page.setCookie(pair)
    //     }))
    // }

    async camouflageBrowser(){
        //删掉webdriver属性 以免被检测出为无头浏览器
        await this.page.evaluateOnNewDocument(() => {
            const newProto = navigator.__proto__;
            delete newProto.webdriver;
            navigator.__proto__ = newProto;
        });

        //无头浏览器模式下,全局对象下的chrome对象是没有 runtime属性的
        await this.page.evaluateOnNewDocument(() => {
            window.navigator.chrome = {
                runtime: {}
            };
        });
    }
}

module.exports = testWeb;
// export default testWeb
